/**
 * Created by Administrator on 2017/3/16.
 */

var mysql = require("mysql");

var MianMysql;

//链接数据库(第一步)
exports.open_db=myopen;
function myopen(){
    MianMysql = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"test"
    })
}



function open_db_new(){
    MianMysql = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"test"
    })
}

exports.db_gamelogin = function(username,res){

    var sql1= "select * from gameuser where user = ?;"
    MianMysql.query(sql1,[username],function(error,data1){
        if(error){
            console.log(error);
            res.send("后台有错，请联系管理员！");
        }else{
           if(data1.length<=0){
               myopen();

               var day = new Date();
               day = (day.getFullYear()+"年"+(day.getMonth()+1)+"月"+day.getDate())+"日";
               var sql2 = "insert into gameuser (user,num,day) VALUE (?,?,?);"
               MianMysql.query(sql2,[username,0,day],function(error,data2){
                   if(error){
                       console.log(error);
                   }else{
                       console.log("增加一条语句 成功");
                       //console.log(data);
                       res.send({node:1,user:data2});
                   }
               });

               myend();
           }else{
               console.log("已经有用户了");
               res.send({node:1,user:data1});
           }
        }
    });

}


exports.db_gamenum = function(user,num,res){
    var sql1= "select num from gameuser where user = ?;"
    MianMysql.query(sql1,[user],function(error,data1){
        if(error){
            console.log(error);
            res.send("后台有错，请联系管理员！");
        }else{
            console.log(data1);
            var num1 = parseInt(data1[0].num),
                num2 = parseInt(num);

            console.log(num1)
            console.log(num2)
            if(num1 >= num2){
                res.send({newnum:num1});
            }else{
                myopen();
                var day = new Date();
                day = (day.getFullYear()+"年"+(day.getMonth()+1)+"月"+day.getDate())+"日";
                var sql2 = "update gameuser set num = ?,day = ? where user = ?;"
                MianMysql.query(sql2,[num2,day,user],function(error,data2){
                    if(error){
                        console.log(error);
                    }else{
                        console.log("update!!");
                        res.send({newnum:num2,rul:"OK"});
                    }
                });
                myend();
            }




        }
    });

}


exports.db_gamelist = function(req,res){
    var sql = "select * from gameuser  order by num desc  limit 0,10;"
    MianMysql.query(sql,[],function(error,data){
        if(error){
            console.log(error);
        }else{
            console.log("数据查询 成功");
            res.json(data);
        }
    });
}

//增加 r_user的用户
exports.add_regGetSend_table = function(name,pawww,res){
    var sql = "select count(*) as name from r_user where r_name = ?;"
    //打开数据库
    var MianMysql1 = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"test"
    })
    //执行查询数据库
    MianMysql1.query(sql,[name],function(error,data){
        if(error){
            console.log(error);
            res.send("false");
        }else{
            console.log("数据查询 成功");
            console.log(data);
            console.log(data[0].name);
            //查询没有用户的情况
            if(data[0].name == 0){
                add_regGetSend_info(name,pawww,res);
            }else{
                console.log("用户名已存在");
                res.send('add_have');
                res.end();
            }
        }
    });

    //执行查询数据库
    MianMysql1.end();

}
function add_regGetSend_info(name,paw,res){
    var sql2 = "insert into r_user (r_name,r_paw) VALUE (?,?);"
    var MianMysql2 = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"test"
    })
    MianMysql2.query(sql2,[name,paw],function(error,data){
        if(error){
            console.log(error);
            res.send("false");
        }else{
            console.log("数据添加 成功");
            console.log(data);
            res.send('add_true');
        }
    });
    MianMysql2.end();
}
//查询数据库中的 所有信息
exports.select_logPostSend_table = select_logPostSend_fun;

function select_logPostSend_fun(req,res){
    var sql = "select r_name,r_paw from r_user ;"
    MianMysql.query(sql,[],function(error,data){
        if(error){
            console.log(error);
        }else{
            //console.log(data);
            //res.send(data);
            res.json(data);
        }
    });
}





//关闭数据库 (第三步)
exports.end_db = myend;
function myend(){
    MianMysql.end();
}


//处理数据库
console.log("this is db!")
exports.db_textGetFormSend = function(t1,t2,res){
    console.log("处理的一条数据");
      //响应部分
    //res.send(str);

    res.send(str);
}